package com.martinwj.camera2demo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.martinwj.camera2demo.R;
import com.martinwj.camera2demo.utils.ImageUtils;

import java.util.List;

/**
 *   功能点的Adapter 功能点的适配器
 *        用来获取指定位置要显示的View  当要显示一个View就调用一次这个方法
 */
public class FuncHorizontalScrollAdapter {

    private Context mContext;
    private LayoutInflater mInflate;
    private List<Integer> mDatas;

    public FuncHorizontalScrollAdapter(Context context, List<Integer> mDatas) {
        this.mContext = context;
        mInflate = LayoutInflater.from(context);
        this.mDatas = mDatas;
    }

    public int getCount() {
        return mDatas.size();
    }

    public Object getItem(int position) {
        return mDatas.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    /**
     *
     * @param position 显示的位置
     * @param convertView  android 为我们做的缓存机制
     * @param parent
     * @return   每个Item都是通过getView返回显示的
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;// ViewHolder保留对子视图的引用
        /**
         * 当convertView不为null时，我们可以直接重用它，没有必要
         * 重新充气。只有当convertView提供时，我们才会对新视图进行充气
         */
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflate.inflate(R.layout.activity_index_gallery_item, parent, false);
            viewHolder.mImg = (ImageView) convertView.findViewById(R.id.id_index_gallery_item_image);
            viewHolder.mText = (TextView) convertView.findViewById(R.id.id_index_gallery_item_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mImg.setImageBitmap(ImageUtils.getThumbBitmap(ImageUtils.mContext, mDatas.get(position)));
        viewHolder.mText.setText("info");

        return convertView;
    }

    private class ViewHolder
    {
        ImageView mImg;
        TextView mText;
    }
}


